<?php
namespace Modules\Stats\Entities;

use Modules\Core\Entities\BaseEntity as Model;

use Modules\User\Entities\User;

use Modules\Asset\Entities\WithdrawRecord;
// use Modules\Asset\Entities\RedpackRecord;
// use Modules\Asset\Entities\ScoreRecord;
use Modules\Asset\Entities\AssetRecord;
use Modules\Asset\Entities\Asset;
use Modules\Order\Entities\BaseOrder as Order;

use ThrowException;

// 用户类
class PlatformStats extends Model
{
    protected $guarded = [];
    protected $table = 'stats_platform';
    protected $appends = [];

    protected $casts = [
    ];

    static public $resourceName = '平台统计';

    static public function beforeGetList($options)
    {
        if (@$options['day']) {
            $options['where']['day'] = $options['day'];
        }

        return $options;
    }

    // 查找或插入某天的数据
    static public function findOrCreate($day)
    {
        return self::where('day', $day)->first() ?: self::create([
            'day' => $day
        ]);
    }

    // 每天凌晨0点时运行记录(只能该时间运行)
    static public function updateAtDayEnd($day) 
    {
        // $day = Date('Y-m-d', strtotime('-1 hours'));
        $item = self::findOrCreate($day);

        // 提现冻结中的余额，也需要计入
        $withdrawPending = WithdrawRecord::status('pending')->sum('value');

        $item->update([
            'all_money' => Asset::whereType('redpack')->sum('balance') + $withdrawPending,
            'all_score' => Asset::whereType('score')->sum('balance'),
            'all_user' => User::count(),
            'active_user' => User::whereDate('last_active_at', $day)->count(),
        ]);
    }


    // 更新某一天的所有指标
    static public function updateAll($day)
    {
        $item = self::findOrCreate($day);

        $item->update([
            'withdraw_money' => WithdrawRecord::status('completed')->whereDate('handled_at', $day)->sum('value'),
            'depot_money' => AssetRecord::where('asset_type', 'redpack')->where('sub_type', 4)->where('type', 1)->whereDate('created_at', $day)->sum('value'),
            'admin_out_money' => AssetRecord::where('asset_type', 'redpack')->whereIn('sub_type', [2, 3])->where('type', 0)->whereDate('created_at', $day)->sum('value'),
            'admin_in_money' => AssetRecord::where('asset_type', 'redpack')->whereIn('sub_type', [2, 3])->where('type', 1)->whereDate('created_at', $day)->sum('value'),
            'admin_out_score' => AssetRecord::where('asset_type', 'score')->whereIn('sub_type', [2, 3])->where('type', 0)->whereDate('created_at', $day)->sum('value'),
            'admin_in_score' => AssetRecord::where('asset_type', 'score')->whereIn('sub_type', [2, 3])->where('type', 1)->whereDate('created_at', $day)->sum('value'),
            'order_money' => Order::paid()->whereDate('money_paid_at', $day)->sum('order_money_price'),
            'order_score' => Order::paid()->whereDate('score_paid_at', $day)->sum('order_score_price'),
            'order_pay_money' => Order::paid()->whereDate('money_paid_at', $day)->sum('pay_money_price'),
            'all_package_money' => 0,
            'all_package_score' => 0
        ]);
    }
}
